<%@page
	import="loginmysql.*,java.util.*, logic.*,java.text.*, java.util.Map,java.util.TreeMap"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
	String resultado = "Hubo un problema, intente";
%>
<%
	MysqlLogin.connect(); // abre la conexion

	//session = request.getSession(false);
	if (session == null || session.getAttribute("loggedIn") == null) {//logoogle
		//not logged
%><a href="FormLogin.jsp">Inicie Sesion</a>
<%
	}else {
		
		
		//logged
%>
<head>
<link rel="stylesheet" href="css/metro-bootstrap.css">
<link rel="stylesheet" href="css/modalcss.css">
<link href="css/iconFont.css" rel="stylesheet">
<link rel="shortcut icon" href="common/favicon.ico" type="image/x-icon">
<link rel="icon" href="common/favicon.ico" type="image/x-icon">
<script src="js/jquery-1.11.1.js"></script>
<script src="js/modal.js"></script>
<meta name="viewport" content="initial-scale=1.0">
<title>Fira</title>

</head>
<jsp:include page="navForm.html"></jsp:include>
<h1 style="text-align: center">Fira</h1>

<body class="metro">
	<div class="container">

		<div id="modal3" class="modalmask" style="margin: 0 auto">
			<div class="modalbox resize">
				<a href="#close" title="Close" class="close"></a>
				<h2 class="centered">Cargando</h2>
				<p class="centered">
					<img src="css/oval.svg" />
				</p>
			</div>
		</div>

		<div class="container">

			<form method=POST action="SaveForm.jsp" id="formSalida"
				onsubmit="validate(event)">
				<%!int itemCount = 0;%>
				<%
					if(session.getAttribute("result")!= null){
																													System.out.println("salidsa resuot"+session.getAttribute("result"));
																														resultado = (String)session.getAttribute("result");
																												//response.sendRedirect("SalidasForm.jsp#modal1");
				%><script type="text/javascript">
					$(document).ready(myFunction);
					function myFunction(test) {
						window.location.hash = '#modal1';
					}
				</script>
				<%
					session.removeAttribute("result");

						}

						itemCount = 0;
						MysqlLogin.clearCats();
						DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
						Date date = new Date();
						String fecha = dateFormat.format(date);
						//out.println(session.getAttribute("FullName"));

						ArrayList<String> unidades = null;
						unidades = MysqlLogin.getUnidades();

						Map<Integer, String> localidadesal = MysqlLogin
								.getLocalidades();

						session.setAttribute("fecha", fecha);
				%>

				<div id="modal1" class="modalmask" style="margin: 0 auto">
					<div class="modalbox movedown">
						<a href="#close" title="Close" class="close">X</a>
						<h2 class="centered"><%=resultado%></h2>
					</div>
				</div>
				<span>Unidad: </span> <select name="unidades">
					<%
						for (String unobject : unidades) {
								//System.out.println(object);
					%><option value="<%=unobject%>"><%=unobject%></option>
					<%
						}
					%>
				</select> <span> Lugar: <select name="localidad">
						<%
							for (java.util.Map.Entry<Integer, String> loc : localidadesal
										.entrySet()) {
									System.out.println(loc.getValue());
						%><option value="<%=loc.getKey()%>"><%=loc.getValue()%></option>
						<%
							}
						%>
				</select></span>
				<!--div class='span2'><select name="tipo">
					<option value="0">Entrada</option>
					<option value="1">Salida</option>
				</select></div> -->

				<span> Fecha: <%=fecha%></span> <span> <%=session.getAttribute("FullName")%></span><br>
				<!-- <span>Turno </span><input type="time" name="turno" required> <span>Hora
			de Salida </span><input type="time" name="salida" required><br> -->
				<span>Hora de Control </span><input type="time" name="control"
					required><span class='aclaracion'>*Si el item es
					insatisfactorio debe poner observaci&oacute;n</span>
				<hr width="100%" align="left">
				<%
					ArrayList<Categoria> cats = null;
						cats = MysqlLogin.getCats();
						session.setAttribute("catArray", cats);
						for (Categoria cat : cats) {
							//System.out.println(cat.getInfo() + " " + cat.getId());
				%><h1><%=cat.getInfo()%></h1>
				<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
					url="jdbc:mysql://localhost/cot" user="root" password="Beyer9499" />
				<c:catch var="catchException">
					<sql:query dataSource="${snapshot}" var="result">
					Select nombre, cid, status.sid, activo from status, statrel where status.sid=statrel.sid and statrel.cid=<%=cat.getId()%>
					</sql:query>
					<div id="no-more-tables">
						<table class='table hovered'>
							<thead class="cf">
								<tr>
									<th class="text-left">Item</th>
									<th class="text-left">Ok</th>
									<c:forEach var="row" items="${result.rows}">
										<c:if test="${row.activo==1}">
											<th class="text-left"><c:out value="${row.nombre}" /></th>
										</c:if>
									</c:forEach>
									<th class="text-left">No aplica</th>
									<th class="text-left">Observaciones</th>
								</tr>
								<%
									ArrayList<Item> items = cat.getItems();
												itemCount += items.size();
												System.out.println(itemCount);
												for (Item itemobject : items) {
								%>
							</thead>
							<tbody>

								<tr>
									<td data-title='Item'><label><%=itemobject.getInfo()%></label></td>
									<td data-title='Satisfactorio'>
										<div class="input-control radio default-style">
											<label> <input id="noRadio" type="radio"
												name="<%=itemobject.getGroup()%>" value="0" required><span
												class="check"></span>
											</label>
										</div>
									</td>

									<c:forEach var="row" items="${result.rows}">
									<c:if test="${row.activo==1}">
										<td data-title=<c:out value="${row.nombre}" />>
											<div class="input-control radio default-style">
												<label> <input type="radio"
													name="<%=itemobject.getGroup()%>"
													value=<c:out value="${row.sid}" /> required><span
													class="check"></span>
												</label>
											</div>
										</td>
										</c:if>
									</c:forEach>

									<td data-title='No aplica'>
										<div class="input-control radio default-style">
											<label> <input type="radio"
												name="<%=itemobject.getGroup()%>" value="-1" required><span
												class="check"></span>
											</label>
										</div>
									</td>

									<td data-title='Observaci&oacute;n'><input
										id="<%=itemobject.getObs()%>" style="width: 100%;"
										maxlength="254" name="<%=itemobject.getObs()%>"
										autocomplete="off" type="text" /></td>
								</tr>
							</tbody>
							<%
								}
							%>

						</table>
				</c:catch>
		</div>
		<%
			}
				session.setAttribute("numItems", itemCount);
		%>


		<input id="form-submit-button" type=SUBMIT value="Enviar" "/>
		</form>
	</div>
</body>

<%
	}

	MysqlLogin.disconnect(); // cierra la conexion
%>
</html>